package com.keycloak.admin.utils;

import com.keycloak.admin.constants.AppConstants;
import com.keycloak.admin.exception.RequestError;
import com.keycloak.admin.exception.RequestException;
import com.keycloak.admin.model.base.VCodeModel;
import com.keycloak.admin.services.VCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * YuedaoXingApi
 * com.ydx.api.locallibs.utils
 * Created by Chris Chen
 * 2017/11/9
 * Explain:检验工具
 */
public class CheckUtils {
    @Autowired
    VCodeService vCodeService;

    /**
     * 检查手机号码是否有效
     *
     * @param cell
     * @return
     */
    public static boolean isCellEffective(String cell) {
        //1. 检查长度是否为11位
        if (cell.length() != 11) {
            //如果手机号码无效，则抛出异常
            throw new RequestException(RequestError.REQUEST_ERROR_PHONE_NUMBER_ERROR);
        }

        return true;
    }

    public static boolean isVCodeEffective(String cell, String vcode, Map<String, VCodeModel> map) {
        //检查是否为这个手机发过短信验证码
        isVCodeExist(cell, vcode, map);
        //验证短信验证码是否失效
        isVCodeInvalide(cell, map);
        return true;
    }

    /**
     * 检查是否为这个手机发过短信验证码
     *
     * @param cell
     * @param map
     * @return
     */
    public static boolean isVCodeExist(String cell, String vcode, Map<String, VCodeModel> map) {
        //以下异常要重新界定
        if (StringUtils.isEmpty(cell)) {
            //手机号码为空
            throw new RequestException(RequestError.REQUEST_ERROR_PHONE_NUMBER_ERROR);
        }
        if (StringUtils.isEmpty(vcode)) {
            //验证码为空
            throw new RequestException(RequestError.REQUEST_ERROR_VCODE_ERROR);
        }
        if (map == null) {
            //该手机还没有获取过短信验证码，抛异常
            throw new RequestException(RequestError.REQUEST_ERROR_VCODE_IS_NOT_EXIST);
        }
        if (map.get(cell) == null) {
            //该手机还没有获取过短信验证码，抛异常
            throw new RequestException(RequestError.REQUEST_ERROR_VCODE_IS_NOT_EXIST);
        }
        if (!vcode.equalsIgnoreCase(map.get(cell).getCode())) {
            //短信验证码不匹配，抛异常
            throw new RequestException(RequestError.REQUEST_ERROR_VCODE_ERROR);
        }

        return true;
    }

    /**
     * 检查验证码是否失效
     *
     * @param cell
     * @param map
     * @return
     */
    public static boolean isVCodeInvalide(String cell, Map<String, VCodeModel> map) {
        //取当前时间和记录时间计算，如果差值超过5分钟即算过期
        long currentTime = System.currentTimeMillis();
        long createTime = map.get(cell).getCreateTime();
        if (currentTime - createTime > AppConstants.VCODE_INVALIDE_TIME * 60 * 1000) {
            throw new RequestException(RequestError.REQUEST_ERROR_VCODE_INVALIDE);
        }
        return false;
    }

    /**
     * 综合检查
     *
     * @param cell
     * @param vcode
     * @param map
     * @return
     */
    public static boolean checkCellAndVCode(String cell, String vcode, Map<String, VCodeModel> map) {
        isCellEffective(cell);//检查手机号码是否有效
        isVCodeExist(cell, vcode, map);//检查手机短信验证码是否存在
        isVCodeInvalide(cell, map);//检查手机短信验证码是否失效
        return true;
    }

    public static final String REGEX_MOBILE = "^1(3[0-9]|5[0-3,5-9]|7[1-3,5-8]|8[0-9])\\d{8}$";
}
